<template>
	<div class="app-container">
		<el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
			<el-form-item label="用户表的用户ID：" prop="userId">
				<el-input v-model.trim="queryParams.userId" placeholder="请输入用户表的用户ID" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品表的商品ID：" prop="goodsId">
				<el-input v-model.trim="queryParams.goodsId" placeholder="请输入商品表的商品ID" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品编号：" prop="goodsSn">
				<el-input v-model.trim="queryParams.goodsSn" placeholder="请输入商品编号" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品名称：" prop="goodsName">
				<el-input v-model.trim="queryParams.goodsName" placeholder="请输入商品名称" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品货品表的货品ID：" prop="productId">
				<el-input v-model.trim="queryParams.productId" placeholder="请输入商品货品表的货品ID" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品货品的价格：" prop="price">
				<el-input v-model.trim="queryParams.price" placeholder="请输入商品货品的价格" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品货品的数量：" prop="number">
				<el-input v-model.trim="queryParams.number" placeholder="请输入商品货品的数量" clearable size="small" />
			</el-form-item>
			<el-form-item label="购物车中商品是否选择状态：" prop="checked">
				<el-input v-model.trim="queryParams.checked" placeholder="请输入购物车中商品是否选择状态" clearable size="small" />
			</el-form-item>
			<el-form-item label="商品图片或者商品货品图片：" prop="picUrl">
				<el-input v-model.trim="queryParams.picUrl" placeholder="请输入商品图片或者商品货品图片" clearable size="small" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button v-hasPermi="['mall:cart:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['mall:cart:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">编辑</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['mall:cart:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button v-hasPermi="['mall:cart:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
			</el-col>
			<right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" stripe :data="cartList" @selection-change="handleSelectionChange">
			<el-table-column :formatter="emptyToString" type="selection" width="55" align="center" />
			<el-table-column :formatter="emptyToString" label="商品图片或者商品货品图片" align="center" prop="id" />
			<el-table-column :formatter="emptyToString" label="用户表的用户ID" align="center" prop="userId" />
			<el-table-column :formatter="emptyToString" label="商品表的商品ID" align="center" prop="goodsId" />
			<el-table-column :formatter="emptyToString" label="商品编号" align="center" prop="goodsSn" />
			<el-table-column :formatter="emptyToString" label="商品名称" align="center" prop="goodsName" />
			<el-table-column :formatter="emptyToString" label="商品货品表的货品ID" align="center" prop="productId" />
			<el-table-column :formatter="emptyToString" label="商品货品的价格" align="center" prop="price" />
			<el-table-column :formatter="emptyToString" label="商品货品的数量" align="center" prop="number" />
			<el-table-column :formatter="emptyToString" label="商品规格值列表，采用JSON数组格式" align="center" prop="specifications" />
			<el-table-column :formatter="emptyToString" label="购物车中商品是否选择状态" align="center" prop="checked" />
			<el-table-column :formatter="emptyToString" label="商品图片或者商品货品图片" align="center" prop="picUrl" />
			<el-table-column :formatter="emptyToString" label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button v-hasPermi="['mall:cart:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
					<el-button v-hasPermi="['mall:cart:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或编辑购物车商品对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="100px">
				<el-form-item label="用户表的用户ID：" prop="userId">
					<el-input v-model.trim="form.userId" placeholder="请输入用户表的用户ID" />
				</el-form-item>
				<el-form-item label="商品表的商品ID：" prop="goodsId">
					<el-input v-model.trim="form.goodsId" placeholder="请输入商品表的商品ID" />
				</el-form-item>
				<el-form-item label="商品编号：" prop="goodsSn">
					<el-input v-model.trim="form.goodsSn" placeholder="请输入商品编号" />
				</el-form-item>
				<el-form-item label="商品名称：" prop="goodsName">
					<el-input v-model.trim="form.goodsName" placeholder="请输入商品名称" />
				</el-form-item>
				<el-form-item label="商品货品表的货品ID：" prop="productId">
					<el-input v-model.trim="form.productId" placeholder="请输入商品货品表的货品ID" />
				</el-form-item>
				<el-form-item label="商品货品的价格：" prop="price">
					<el-input v-model.trim="form.price" placeholder="请输入商品货品的价格" />
				</el-form-item>
				<el-form-item label="商品货品的数量：" prop="number">
					<el-input v-model.trim="form.number" placeholder="请输入商品货品的数量" />
				</el-form-item>
				<el-form-item label="商品规格值列表，采用JSON数组格式：" prop="specifications">
					<el-input v-model.trim="form.specifications" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="购物车中商品是否选择状态：" prop="checked">
					<el-input v-model.trim="form.checked" placeholder="请输入购物车中商品是否选择状态" />
				</el-form-item>
				<el-form-item label="商品图片或者商品货品图片：" prop="picUrl">
					<el-input v-model.trim="form.picUrl" placeholder="请输入商品图片或者商品货品图片" />
				</el-form-item>
				<el-form-item label="删除标志：" prop="delFlag">
					<el-input v-model.trim="form.delFlag" placeholder="请输入删除标志" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">保 存</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listCart, getCart, delCart, addCart, updateCart, exportCart } from '@/api/mall/cart';

export default {
	name: 'Cart',
	components: {},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 购物车商品表格数据
			cartList: [],
			// 弹出层标题
			title: '',
			// 是否显示弹出层
			open: false,
			// 搜索参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				userId: null,
				goodsId: null,
				goodsSn: null,
				goodsName: null,
				productId: null,
				price: null,
				number: null,
				specifications: null,
				checked: null,
				picUrl: null,
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 搜索购物车商品列表 */
		getList() {
			this.loading = true;
			listCart(this.queryParams).then((response) => {
				this.cartList = response.data.records;
				this.total = response.data.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				userId: null,
				goodsId: null,
				goodsSn: null,
				goodsName: null,
				productId: null,
				price: null,
				number: null,
				specifications: null,
				checked: null,
				picUrl: null,
				delFlag: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
			};
			this.resetForm('form');
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm('queryForm');
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map((item) => item.id);
			this.single = selection.length !== 1;
			this.multiple = !selection.length;
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = '添加购物车商品';
		},
		/** 编辑按钮操作 */
		handleUpdate(row) {
			this.reset();
			const id = row.id || this.ids;
			getCart(id).then((response) => {
				this.form = response.data;
				this.open = true;
				this.title = '编辑购物车商品';
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs['form'].validate((valid) => {
				if (valid) {
					if (this.form.id != null) {
						updateCart(this.form).then((response) => {
							this.msgSuccess('编辑成功');
							this.open = false;
							this.getList();
						});
					} else {
						addCart(this.form).then((response) => {
							this.msgSuccess('新增成功');
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids;
			this.$confirm('是否确认删除购物车商品编号为"' + ids + '"的数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return delCart(ids);
				})
				.then(() => {
					this.getList();
					this.msgSuccess('删除成功');
				});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有购物车商品数据项?', '警告', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(function () {
					return exportCart(queryParams);
				})
				.then((response) => {
					this.download(response.msg);
				});
		},
	},
};
</script>
